package com.phiture.erp.basic.core.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.phiture.erp.basic.core.pojo.dto.ErpMaterialPurchasingPageReqDTO;
import com.phiture.erp.basic.core.pojo.dto.ErpMaterialPurchasingSaveReqDTO;
import com.phiture.erp.basic.core.pojo.vo.ErpMaterialPurchasingRespVO;
import com.phiture.erp.basic.core.service.ErpMaterialPurchasingService;
import com.phiture.erp.basic.dal.entity.ErpMaterialPurchasingDO;
import com.phiture.erp.basic.dal.mapper.ErpMaterialPurchasingMapper;
import com.phiture.erp.common.enums.ErpMaterialPurchasingCheckTypeEnum;
import com.phiture.erp.common.enums.ErpMaterialPurchasingStatusEnum;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.phiture.erp.common.constants.ErrorCodeConstants.MATERIAL_PURCHASING_NOT_EXISTS;


/**
 * 物料采购属性 Service 实现类
 *
 * @author 芋道源码
 */
@Service
public class ErpMaterialPurchasingServiceImpl implements ErpMaterialPurchasingService {

    @Resource
    private ErpMaterialPurchasingMapper materialPurchasingMapper;

    @Override
    public Long createMaterialPurchasing(ErpMaterialPurchasingSaveReqDTO createReqDTO) {
        // 插入
        ErpMaterialPurchasingDO materialPurchasing = BeanUtils.toBean(createReqDTO, ErpMaterialPurchasingDO.class);
        materialPurchasingMapper.insert(materialPurchasing);
        // 返回
        return materialPurchasing.getId();
    }

    @Override
    public void updateMaterialPurchasing(ErpMaterialPurchasingSaveReqDTO updateReqDTO) {
        // 校验存在
        validateMaterialPurchasingExists(updateReqDTO.getId());
        // 更新
        ErpMaterialPurchasingDO updateObj = BeanUtils.toBean(updateReqDTO, ErpMaterialPurchasingDO.class);
        materialPurchasingMapper.updateById(updateObj);
    }

    @Override
    public void createOrUpdateMaterialPurchasing(ErpMaterialPurchasingSaveReqDTO reqDTO) {
        boolean exists = materialPurchasingMapper.exists(
                Wrappers.lambdaQuery(ErpMaterialPurchasingDO.class)
                        .eq(ErpMaterialPurchasingDO::getMaterialId, reqDTO.getMaterialId())
                        .eq(ErpMaterialPurchasingDO::getDeleted, Boolean.FALSE)
        );
        if(exists){
            updateMaterialPurchasing(reqDTO);
        }else {
            createMaterialPurchasing(reqDTO);
        }
    }

    @Override
    public void deleteMaterialPurchasing(Long id) {
        // 校验存在
        validateMaterialPurchasingExists(id);
        // 删除
        materialPurchasingMapper.deleteById(id);
    }

    private void validateMaterialPurchasingExists(Long id) {
        if (materialPurchasingMapper.selectById(id) == null) {
            throw exception(MATERIAL_PURCHASING_NOT_EXISTS);
        }
    }

    @Override
    public ErpMaterialPurchasingRespVO getMaterialPurchasingByMaterialId(Long materialId) {
        ErpMaterialPurchasingDO materialPurchasingDO = materialPurchasingMapper.selectOne(
                Wrappers.lambdaQuery(ErpMaterialPurchasingDO.class)
                        .eq(ErpMaterialPurchasingDO::getMaterialId, materialId)
                        .eq(ErpMaterialPurchasingDO::getDeleted, Boolean.FALSE)
        );
        return BeanUtils.toBean(
                materialPurchasingDO,
                ErpMaterialPurchasingRespVO.class,
                info ->
                        info.setStatusDesc(ErpMaterialPurchasingStatusEnum.convert(info.getStatus()))
                        .setPurchaseCheckTypeDesc(ErpMaterialPurchasingCheckTypeEnum.convert(info.getPurchaseCheckType()))
        );
    }

    @Override
    public PageResult<ErpMaterialPurchasingDO> getMaterialPurchasingPage(ErpMaterialPurchasingPageReqDTO pageReqDTO) {
        return materialPurchasingMapper.selectPage(pageReqDTO, new LambdaQueryWrapperX<ErpMaterialPurchasingDO>()
                .eqIfPresent(ErpMaterialPurchasingDO::getStatus, pageReqDTO.getStatus())
                .eqIfPresent(ErpMaterialPurchasingDO::getMaterialId, pageReqDTO.getMaterialId())
                .eqIfPresent(ErpMaterialPurchasingDO::getPurPersonId, pageReqDTO.getPurPersonId())
                .eqIfPresent(ErpMaterialPurchasingDO::getSupplierId, pageReqDTO.getSupplierId())
                .eqIfPresent(ErpMaterialPurchasingDO::getPurchaseCheckType, pageReqDTO.getPurchaseCheckType())
                .betweenIfPresent(ErpMaterialPurchasingDO::getCreateTime, pageReqDTO.getCreateTime())
                .orderByDesc(ErpMaterialPurchasingDO::getId));
    }

    @Override
    public Map<Long, ErpMaterialPurchasingDO> getErpMaterialPurchasingMap(Collection<Long> materialIds) {
        List<ErpMaterialPurchasingDO> list = materialPurchasingMapper.selectList(ErpMaterialPurchasingDO::getMaterialId, materialIds);
        return CollUtil.isEmpty(list)
                ? Collections.emptyMap()
                : list.stream().collect(Collectors.toMap(ErpMaterialPurchasingDO::getMaterialId, obj -> obj));
    }

}